//
//  BYAdminHeaderView.m
//  School+
//
//  Created by Sean Sebastian on 5/15/16.
//  Copyright © 2016 Sean Sebastian. All rights reserved.
//

#import "BYAdminHeaderView.h"

#import "UIImageView+WebCache.h"
#import "Masonry.h"

#import "AdminAccount.h"

@interface BYAdminHeaderView ()

@property (nonatomic , weak) UIImageView *iconView;
@property (nonatomic , weak) UILabel *userName;
@property (nonatomic , weak) UIImageView *backgroundImageView;

@property (nonatomic , weak) UIButton *logoutBtn;

@end

@implementation BYAdminHeaderView


- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    [self prepareUI];
    
    return self;
}

- (void)prepareUI
{
    [self addSubview:self.backgroundImageView];
    [self addSubview:self.iconView];
    [self addSubview:self.userName];
    [self addSubview:self.logoutBtn];
    
    [self.backgroundImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.equalTo(self);
    }];
    
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.center.equalTo(self);
        make.width.mas_equalTo(@100);
        make.height.mas_equalTo(@100);
    }];
    [self.userName mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(self.iconView.mas_bottom).with.offset(10);
        make.centerX.equalTo(self.iconView);
    }];
    
    // 1、先创建一个玻璃效果
    UIBlurEffect *blur = [UIBlurEffect effectWithStyle:UIBlurEffectStyleDark];
    // 2、根据这个毛玻璃效果来创建 UIVisualEffectView
    UIVisualEffectView *effcetView = [[UIVisualEffectView alloc] initWithEffect:blur];
    [self.backgroundImageView addSubview:effcetView];
    
    [effcetView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.edges.mas_equalTo(UIEdgeInsetsMake(0, 0, 0, 0));
    }];
    
    
    [self.logoutBtn mas_makeConstraints:^(MASConstraintMaker *make) {
        make.right.equalTo(self).with.offset(-20);
        make.top.equalTo(self).with.offset(20);
        make.width.and.height.mas_equalTo(@40);
    }];
}

- (void)logoutBtnClick
{
    [self.delegate logoutBtnClick];
}

- (UIImageView *)iconView
{
    if (_iconView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        
        NSURL *url = [NSURL URLWithString:[AdminAccount sharedAdminAccount].iconImageURL];
        [imageView sd_setImageWithURL:url];
        
        imageView.layer.masksToBounds = YES;
        imageView.layer.cornerRadius = 50;
        
        _iconView = imageView;
        return imageView;
    }
    return _iconView;
}

- (UILabel *)userName
{
    if (_userName == nil) {
        UILabel *label = [[UILabel alloc] init];
        label.text = [AdminAccount sharedAdminAccount].username;
        label.textColor = [UIColor whiteColor];
        _userName = label;
        return label;
    }
    return _userName;
}

- (UIImageView *)backgroundImageView
{
    if (_backgroundImageView == nil) {
        UIImageView *imageView = [[UIImageView alloc] init];
        
        NSURL *url = [NSURL URLWithString:[AdminAccount sharedAdminAccount].iconImageURL];
        [imageView sd_setImageWithURL:url];
        _backgroundImageView = imageView;
        return imageView;
    }
    return _backgroundImageView;
}

- (UIButton *)logoutBtn
{
    if (_logoutBtn == nil) {
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeInfoLight];
        [btn addTarget:self action:@selector(logoutBtnClick) forControlEvents:UIControlEventTouchUpInside];
        btn.backgroundColor = [UIColor clearColor];
        _logoutBtn = btn;
        return btn;
    }
    return _logoutBtn;
}

@end
